package com.bdqn.vueboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.vueboot.config.BascText;
import com.bdqn.vueboot.config.CustomException;
import com.bdqn.vueboot.dto.CarDto;
import com.bdqn.vueboot.entity.*;
import com.bdqn.vueboot.mapper.CarMapper;
import com.bdqn.vueboot.mapper.DriverMapper;
import com.bdqn.vueboot.mapper.SysUserMapper;
import com.bdqn.vueboot.mapper.VehicleMapper;
import com.bdqn.vueboot.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car>
        implements CarService {

    @Autowired
    CarService carservice;
    @Autowired
    CarMapper carMapper;

    @Autowired
    DepartmentService departmentService;
    @Autowired
    VehicleService vehicleService;
    @Autowired
    VehicleMapper vehicleMapper;
    @Autowired
    SysUserService sysUserService;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    StaticService staticService;
    @Autowired
    DriverMapper driverMapper;

    @Override
    public IPage listBiz(IPage<Car> iPage, CarDto carData) {
        List<CarDto> carDtoList = new ArrayList<>();
        LambdaQueryWrapper<Car> query = new LambdaQueryWrapper<>();
        if (carData.getContactsName() != null && carData.getContactsName() != "") {
            LambdaQueryWrapper<SysUser> query2 = new LambdaQueryWrapper<>();
            query2.like(SysUser::getUsrName, carData.getContactsName());//联系人
            SysUser byId1 = sysUserService.getOne(query2);
            query.eq(byId1.getUsrId() != 0, Car::getContacts, byId1.getUsrId());
        }

  /* @RequestParam(value = "destination", required = false) String destination,//目的地
                  @RequestParam(value = "proposerName", required = false) String proposerName,//申请人名称
                  @RequestParam(value = "contactsName", required = false) String contactsName,//联系人
                  @RequestParam(value = "departureTime", required = false) Data departureTime,//发车时间
                  @RequestParam(value = "flybackTime", required = false) Data flybackTime//回程时间,*/

        query.like(carData.getProposer() != null, Car::getProposer, carData.getProposer());
        query.like(carData.getDestination() != null, Car::getDestination, carData.getDestination());
        query.ge(carData.getDepartureTime() != null, Car::getDepartureTime, carData.getDepartureTime());
        query.le(carData.getFlybackTime() != null, Car::getFlybackTime, carData.getFlybackTime());
        query.isNull(carData.getStaticId() == 1, Car::getStaticId);
        query.orderByDesc(Car::getCreationTime);
        IPage<Car> page = carservice.page(iPage, query);
        if (page.getRecords() == null) {
            throw new CustomException("没查询到数据！");
        }
        IPage<CarDto> iPage1 = new Page<>();
        BeanUtils.copyProperties(page, iPage1);
//        carDtoList = iPage1.getRecords();
//        BeanUtils.copyProperties(iPage1.getRecords(),carDtoList);

        for (Car car : iPage1.getRecords()
        ) {
            CarDto carDto = new CarDto();
            BeanUtils.copyProperties(car, carDto);
            carDtoList.add(carDto);
        }

        carDtoList = carDtoList.stream().map((item) -> {
            SysUser byId = sysUserService.getById(item.getContacts());
            item.setContactsName(byId.getUsrName());
            Vehicle byId2 = vehicleService.getById(item.getVehicle());
            Department byId3 = departmentService.getById(item.getDepartmentId());
            item.setDepartmentName(byId3.getDepartmentName());
            item.setVehicleName(byId2.getVehicleName());
            Static byId4 = staticService.getById(item.getStaticId());
            if (byId4 != null) {
                item.setStaticName(byId4.getStaticName());
            }
            if (item.getOvernight() == 0) {
                item.setOvernightName("否");
            } else {
                item.setOvernightName("是");
            }
            return item;
        }).collect(Collectors.toList());
        iPage1.setRecords(carDtoList);
        return iPage1;
    }

    @Override
    @Transactional
    public int add(CarDto carDto) {
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();

        sysUserLambdaQueryWrapper.eq(SysUser::getUsrName, carDto.getContactsName());
        SysUser one = sysUserService.getOne(sysUserLambdaQueryWrapper);
        if (one == null) {
            throw new CustomException("没有该联系人！！");
        }
        Car car = new Car();
        BeanUtils.copyProperties(carDto, car);
        car.setContacts(one.getUsrId());
        car.setDepartmentId(one.getDepId());
        LambdaQueryWrapper<Vehicle> vehicleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        vehicleLambdaQueryWrapper.eq(Vehicle::getVehicleEmpty, 0);
        List<Vehicle> vehicles = vehicleMapper.selectList(vehicleLambdaQueryWrapper);
        Vehicle vehicle1 = new Vehicle();
        for (Vehicle vehicle : vehicles
        ) {
            vehicle1 = vehicle;
            Vehicle vehicle2 = new Vehicle();
            vehicle2.setVehicleId(vehicle1.getVehicleId());
            vehicle2.setVehicleEmpty(1);
            vehicleMapper.updateById(vehicle2);
            break;
        }
        car.setVehicle(vehicle1.getVehicleId());


        LambdaQueryWrapper<Driver> driverLambdaQueryWrapper = new LambdaQueryWrapper<>();
        driverLambdaQueryWrapper.eq(Driver::getDriverUsable, 0);
        List<Driver> driverList = driverMapper.selectList(driverLambdaQueryWrapper);
        Driver driver1 = new Driver();
        for (Driver driver : driverList
        ) {
            driver1 = driver;
            Driver driver2 = new Driver();
            driver2.setDriverId(driver.getDriverId());
            driver2.setDriverUsable(2);
            driverMapper.updateById(driver2);
            break;
        }
        car.setDriverId(driver1.getDriverId());

        SysUser sysUser = sysUserMapper.selectById(BascText.getCarId());
        if (sysUser == null) {
            throw new CustomException("请重新登录！！");
        }
        if (Integer.parseInt(car.getDriver()) == 0) {
            car.setDriver("全程");
        } else if (Integer.parseInt(car.getDriver()) == 1) {
            car.setDriver("接人");

        } else {
            car.setDriver("送");

        }
        car.setProposer(sysUser.getUsrName());
        car.setStartingTime(new Date(System.currentTimeMillis()));
        car.setStaticId(1);
        int insert = carMapper.insert(car);
        return insert;
    }

    @Override
    public int deleteById(int id) {
        Car byId;
        Car car = new Car();
        try {
            byId = carservice.getById(id);
            Vehicle vehicle = new Vehicle();
            vehicle.setVehicleId(byId.getVehicle());
            vehicle.setVehicleEmpty(3);
            vehicleMapper.updateById(vehicle);
            Driver driver = new Driver();
            driver.setDriverId(byId.getDriverId());
            driver.setDriverUsable(3);
            driverMapper.updateById(driver);
            car.setId(byId.getId());
            car.setStaticId(8);
            car.setDriverId(byId.getDriverId());
            car.setVehicle(byId.getVehicle());
            car.setContacts(byId.getContacts());
        } catch (Exception e) {
            throw new CustomException("信息错误！");
        }
        int i = carMapper.updateById(car);
        if (i > 0) {
            return i;
        } else {
            throw new CustomException("修改错误！");
        }
    }

    @Override
    public int sevaAudit(Integer staticId, Long id) {
        UpdateWrapper<Car> updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        Car car = new Car();
        car.setStaticId(staticId);
        return carMapper.update(car, updateWrapper);
    }
}




